<?php defined('SYSPATH') or die('No direct access allowed.');
class Auth
{
	/**
	 * @var string current Uri
	 */
	private $_currentUri = null;

	/**
	 * @var boolean
	 */
	private $_isAdmin = null;

	/**
	 * @var array
	 */
	private $_notNeedLoginPages = array(
		'user/login',
		'user/auth',
		'user/captcha',
		'game/click'
	);

	/**
	 * @var array
	 */
	private $_onlyAdminAccessPages = array(
		'user/del',
		'user/edit',
		'user/index',
		'game/delimg',
	);

	public function  __construct()
	{
		$this->_currentUri = Request::$current->controller.'/'.
			Request::$current->action;

		$this->_isAdmin = Func::sess('isAdmin');
	}

	public function checkLogin()
	{
		if ($this->inNotNeedLoginPages()) return true;

		return (bool) Func::sess('id');
	}

	private function inNotNeedLoginPages()
	{
		return in_array($this->_currentUri, $this->_notNeedLoginPages);
	}	

	public function allowAccess()
	{
		if (!$this->checkLogin()) return false;

		if ($this->_isAdmin) return true;

		if (in_array($this->_currentUri, $this->_onlyAdminAccessPages)) return false;

		return true;
	}
}